﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IMedicare;
using System.Collections;
using BusinessLibary;
using EntityLibrary;

namespace Medicare
{
    public partial class FrmInhosRegister : Form
    {
        public FrmInhosRegister()
        {
            InitializeComponent();
            registerInfoFacade = new RegisterInfoFacade();
            unregistPaient = new DataSet();
            registedPaient = new DataSet();
        }

        private IMedicare.IMedicare medicare;
        private int retint = 0;
        private string syxh = string.Empty;//首页序号
        private ArrayList PrenoList = new ArrayList();
        private string medicareno = string.Empty;
        private string idcardNo = string.Empty;

        private DataSet paientInfo;
        private DataSet unregistPaient;
        private DataSet registedPaient;


        private RegisterInfoFacade registerInfoFacade;
        private DataView dv1 = new DataView();
        private DataView dv2 = new DataView();
        private DataView dv3 = new DataView();

        private void FrmInhosRegister_Load(object sender, EventArgs e)
        {
            medicare = IMedicare.IMedicareFactory.Instance.CreateIMedicare();//s
            ArrayList list = new ArrayList();
            retint = medicare.InitHisDLL(list);
            if (retint != 0)
            {
                MessageBox.Show("医保接口初始化失败，请检查医保网络！");
                return;
            }

            DataBlind();
        }

        private void DataBlind()
        {

            try
            {
                paientInfo = registerInfoFacade.FindAllMedicarePatientInfo();
                unregistPaient = registerInfoFacade.FindbyUnRegistPaientInfo();
                registedPaient = registerInfoFacade.FindbyRegistedPatientInfo();
                dv1 = paientInfo.Tables[0].DefaultView;
                dv2 = unregistPaient.Tables[0].DefaultView;
                dv3 = registedPaient.Tables[0].DefaultView;
                gridControl1.DataSource = dv1;
                gridControl2.DataSource = dv2;
                gridControl3.DataSource = dv3;

            }
            catch
            {

                MessageBox.Show("连接HIS服务器失败！请检查医院网络或联系管理员！");
            }

        }
        private void btnRegist_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            xtraTabControl1.SelectedTabPage = xtraTabPage1;
            int rowhandle = gridView2.FocusedRowHandle;
            //string inhosID=lookUpEditEx1.DisplayValue;//InhosID 从哪张dataview中取？
            //cardno
            if (rowhandle <= -1)
            {
                MessageBox.Show("请选择要进行医保登记的病人！");
                return;
            }

            //371100235160|3711002877|371122199510190035|张景栋|1|01|19951019000000||||80||||||莒县五中|371122|96||61|0|2008|0|||||0|0|0|||0|1|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|||0|||||||||||||371122|^

            ShowPresonInfo();


            DataRow row = gridView2.GetDataRow(rowhandle);
            string inhosID = row["blh"].ToString();//InhosID 
            //if (!medicareno.Equals(row["cardno"].ToString()))
            //{
            //    MessageBox.Show("登记的医保号和读卡获取的医保号不符，请重新输入医保号完成登记！");
            //    return;
            //}
            DataSet inhosRecordData = BuildInhosRecord(row);//构造住院登记信息实体
            //DataTable mytable=new DataTable();

            //mytable.ImportRow(row);
            //inhosRecordData.Tables.Add(mytable);

            //inhosRecordData.WriteXml("C://aaa.xml");

            //ArrayList PrenoList = new ArrayList();
            retint = medicare.InHosRegister(1, 0, inhosID, 0, inhosRecordData, null, ref PrenoList);
            if (retint != 0)
            {
                MessageBox.Show("登记失败！");
                return;
            }
            MessageBox.Show("登记成功！");


        }


        public void ShowPresonInfo()
        {
            DataSet paientInfo = new DataSet();
            ArrayList PrenoList = new ArrayList();
            retint = medicare.ReadCard(1, "", ref paientInfo, ref PrenoList);
            if (retint != 0)
            {
                return;
            }

            string[] split = PrenoList[0].ToString().Split('^')[2].Split('|');
            //371100287582|3711004348|371121200608092328|马芮|2|01|20060809000000||||80||||||五莲县高泽镇中心小学|371121|96||61|0|2009|0|||||0|0|0|||0|1|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|||0|||||||||||||371121|
            string birthdaystr = "1900-01-01";
            try
            {
                //19400827
                //Convert.ToDateTime(split[39].Substring(0, 4) + "-" + split[39].Substring(4, 2) + "-" + split[39].Substring(6, 2));
                birthdaystr = split[6].Substring(0, 4) + "-" + split[6].Substring(4, 2) + "-" + split[6].Substring(6, 2);
                //birthdaystr = split[7];                 //生日返回的格式 1900-01-01
            }
            catch (Exception)
            {
                birthdaystr = "1900-01-01";
            }


            FrmPersonInfo frm = new FrmPersonInfo();
            frm.PersonName.Text = split[3];
            frm.Sex.Text = split[4];
            frm.birthday.Text = split[6];
            frm.age.Text = Convert.ToString(System.DateTime.Today.Year - Convert.ToDateTime(birthdaystr).Year);
            frm.MedID.Text = split[0];
            medicareno = split[0];//获取医保号
            frm.CardNo.Text = split[9];
            frm.idcardno.Text = split[2];
            idcardNo = split[2];//身份证号
            frm.unitno.Text = split[16];
            frm.unitname.Text = split[17];
            #region 增加银行卡读卡获取卡余额

            frm.AccountBalance.Text = split[33].ToString();

            PosChantEnity entity = new PosChantEnity();
            entity.MerchanCode = "03";
            entity.MerchanMoney = "";
            entity.POSFlowNo = DateTime.Now.ToString("MMddss");
            entity.CashMemberNo = "";
            entity.CashRegNo = "";
            entity.OrginTerminalNo = "";
            entity.AuthorizationNo = "";
            entity.OrginTranDT = "";
            entity.TerminalType = "7";
            entity.Track1 = "";
            entity.Track2 = "5226085710076865=49121200352600812761";
            entity.Track3 = "996226095710076865=1561560500050000001015812761214000049120=5710076865=000000000=00000000571000000000000";

            frm.AccountBalance.Text = medicare.ShowBalance(entity);
            //frm.AccountBalance.Text = split[33].ToString();
            #endregion
            
            frm.persontype.Text = split[10];
            frm.InHosTimes.Text = split[47].ToString();
            frm.InHosState.Text = split[34] == "1" ? "住院" : "出院";
            frm.textBox16.Text = split[37];//年度帐户支付累计
            frm.textBox15.Text = split[38];//统筹基金支付
            frm.ShowDialog();
        }

        public DataSet BuildInhosRecord(DataRow row)
        {
            DataSet ds = new DataSet();
            DataTable myDataTable = new DataTable();
            DataColumn myDataColumn;

            myDataColumn = new DataColumn();
            myDataColumn.DataType = System.Type.GetType("System.String");
            myDataColumn.ColumnName = "INHOSID";
            myDataTable.Columns.Add(myDataColumn);

            myDataColumn = new DataColumn();
            myDataColumn.DataType = System.Type.GetType("System.String");
            myDataColumn.ColumnName = "INHOSTIME";
            myDataTable.Columns.Add(myDataColumn);

            myDataColumn = new DataColumn();
            myDataColumn.DataType = System.Type.GetType("System.String");
            myDataColumn.ColumnName = "LEAVEHOSTIME";
            myDataTable.Columns.Add(myDataColumn);

            myDataColumn = new DataColumn();
            myDataColumn.DataType = System.Type.GetType("System.String");
            myDataColumn.ColumnName = "ILLNESSNO";
            myDataTable.Columns.Add(myDataColumn);


            myDataColumn = new DataColumn();
            myDataColumn.DataType = System.Type.GetType("System.String");
            myDataColumn.ColumnName = "OFFICEID";
            myDataTable.Columns.Add(myDataColumn);

            myDataColumn = new DataColumn();
            myDataColumn.DataType = System.Type.GetType("System.String");
            myDataColumn.ColumnName = "BEDID";
            myDataTable.Columns.Add(myDataColumn);

            myDataColumn = new DataColumn();
            myDataColumn.DataType = System.Type.GetType("System.String");
            myDataColumn.ColumnName = "DOCTORID";
            myDataTable.Columns.Add(myDataColumn);

            myDataColumn = new DataColumn();
            myDataColumn.DataType = System.Type.GetType("System.String");
            myDataColumn.ColumnName = "OPERATOEID";
            myDataTable.Columns.Add(myDataColumn);

            myDataColumn = new DataColumn();
            myDataColumn.DataType = System.Type.GetType("System.String");
            myDataColumn.ColumnName = "MEDICARENO";
            myDataTable.Columns.Add(myDataColumn);

            myDataColumn = new DataColumn();
            myDataColumn.DataType = System.Type.GetType("System.String");
            myDataColumn.ColumnName = "IDCARDNO";
            myDataTable.Columns.Add(myDataColumn);

            DataRow myDataRow;
            myDataRow = myDataTable.NewRow();

            myDataRow["INHOSID"] = row["blh"];
            myDataRow["INHOSTIME"] = row["ryrq"];
            myDataRow["LEAVEHOSTIME"] = row["cyrq"];
            myDataRow["ILLNESSNO"] = row["zddm"];
            myDataRow["OFFICEID"] = row["ksdm"];
            myDataRow["BEDID"] = row["cwdm"];
            myDataRow["DOCTORID"] = row["ysdm"];
            myDataRow["OPERATOEID"] = row["czyh"];
            myDataRow["MEDICARENO"] = medicareno;
            myDataRow["IDCARDNO"] = idcardNo;

            myDataTable.Rows.Add(myDataRow);
            ds.Tables.Add(myDataTable);

            return ds;

        }


        private void btnQuery_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ShowPresonInfo();
        }

        private void btnCancel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            xtraTabControl1.SelectedTabPage = xtraTabPage2;
            int rowhandle = gridView3.FocusedRowHandle;
            //string inhosID=lookUpEditEx1.DisplayValue;//InhosID 从哪张dataview中取？

            if (rowhandle <= -1)
            {
                MessageBox.Show("请选择要进行取消医保登记的病人！");
                return;
            }

            ShowPresonInfo();

            DataRow row = gridView3.GetDataRow(rowhandle);
            string inhosID = row["INHOSID"].ToString();//InhosID 

            ArrayList PrenoList = new ArrayList();
            retint = medicare.InHosRegister(-1, 0, inhosID, 0, null, null, ref PrenoList);
            if (retint != 0)
            {
                MessageBox.Show("执行取消医保登记失败！");
                return;
            }

            MessageBox.Show("执行取消医保登记成功！");
        }

        private void btnQuit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            PrenoList.Clear();
            medicare.FreeHisDLL(PrenoList);

            this.Close();
        }

        private void xtraTabControl1_SelectedPageChanging(object sender, DevExpress.XtraTab.TabPageChangingEventArgs e)
        {
            //if (xtraTabControl1.SelectedTabPage==xtraTabPage1)
            //{
            //    //更新已登记的病人
            //}
            //if (xtraTabControl1.SelectedTabPage == xtraTabPage2)
            //{
            //    //更新登记的病人
            //}
            DataBlind();
        }

        private void txtInhosID_EditValueChanged(object sender, EventArgs e)
        {
            string rowfilter = "";
            if (!txtInhosID.Text.Equals(string.Empty))
            {
                rowfilter = "blh like '" + txtInhosID.Text + "%'";
            }
            dv1.RowFilter = rowfilter;

            dv2.RowFilter = rowfilter;
        }
    }
}
